clear all
*cap log close
set more off

* suggestive tests for driver learning -----------------------------------------------------

* set parameters --------
local Y = "cite"


* panel a: heterogeneity by stringency exposure ---------------------------------------------
 
* citations with clean past year ----------------------
use "${data}/out/4-main", clear
keep if cite_py1==0

gen DATE = offensedate 
gen ID   = citationid
keep  ID DATE dlnumber
order ID DATE dlnumber

* joining FHP speeding stops 1+ year prior --------------
joinby dlnumber using "${data}/out/2-stopsfhp"

keep if (DATE - offensedate > 365) 
keep if viol_code == 575

gen xspeed = speeddiff if speeddiff >= 9 & speeddiff <= 29
gen harsh = (speeddiff >= 10) if !mi(xspeed)
bysort officerid: egen num   = sum(harsh)
bysort officerid: egen denom = sum(!mi(xspeed))
gen Z  = (num - harsh)/(denom - 1)
replace Z = . if mi(officerid)

gen N =1
gen nonlenient = (lenient==0) & !mi(lenient)
collapse (sum) N lenient nonlenient (mean) Z, by(ID)

* attach to original citation sample --------------------
ren ID citationid 
gen Npast = N
gen Npast_l  = lenient
gen Npast_nl = nonlenient 
keep citationid Npast Npast_l Npast_nl 
merge 1:1 citationid using "${data}/out/4-main", keep(3) nogen

* setup dataset for analysis --------------------------
gen pastnl = (Npast_nl>0)

// for testing difference 
gen W = pastnl 
egen totfe_W = group(totfe W)
gen D    = harsh
gen Z_W1 = Z*(pastnl==1)
gen Z_W0 = Z*(pastnl==0)
gen D_W1 = harsh*(pastnl==1)
gen D_W0 = harsh*(pastnl==0)

// Define outcome
gen Y = `Y'_ny1

* group specific IV estimates --------------------------
ivreghdfe Y (D_W0 D_W1 = Z_W0 Z_W1) W, absorb(totfe) vce(cluster officerid) 
local b0 = "Unexposed: {it:{&beta}{subscript:IV}} = `:di %4.3f _b[D_W0]' (`:di %5.3f _se[D_W0]')"
local b1 = "Exposed: {it:{&beta}{subscript:IV}} = `:di %4.3f _b[D_W1]' (`:di %5.3f _se[D_W1]')"

* test difference across groups ------------------------
ivreghdfe Y (D D_W1 = Z Z_W1) W, absorb(totfe) vce(cluster officerid) nocons resid 
local diff = "{&Delta} = `:di %5.3f _b[D_W1]' (`:di %5.3f _se[D_W1]')"

* setup for plot ----------------------
reghdfe Z W, absorb(totfe) nocons resid 
predict rZ, resid 
reghdfe Y W, absorb(totfe) nocons resid 
predict rY, resid 

* manual binscatter --------------------
binscatter rY rZ, nodraw nq(20) gen(BIN)
gen mZ = .
gen mY1=.
gen mY0=.

forval i = 1/20 {
	qui summ rZ if BIN == `i'
	replace mZ = r(mean) if _n == `i'
	qui summ rY if BIN == `i' & W == 1
	replace mY1 = r(mean) if _n == `i'
	qui summ rY if BIN == `i' & W == 0 
	replace mY0 = r(mean) if _n == `i'
}

* nonparametric fits ------------------------
_pctile rZ, n(100)
scalar loZ = r(r2)
scalar hiZ = r(r98)

// fit for in ----
lpoly rY rZ if W==1 & rZ>loZ & rZ<hiZ,  nogr ci gen(x1 y1) se(y1se) degree(0) bw(0.2) kernel(tri)
cap g y1lb = y1 - 1.96*y1se
cap g y1ub = y1 + 1.96*y1se

// fit for out ----
lpoly rY rZ if W==0 & rZ>loZ & rZ<hiZ, nogr ci gen(x0 y0) se(y0se) degree(0) bw(0.2) kernel(tri)
cap g y0lb = y0 - 1.96*y0se
cap g y0ub = y0 + 1.96*y0se


/// Build graph ------------------------------------
#delimit ;
twoway 
rarea y1lb y1ub x0, fcolor(dknavy%20) lwidth(none)  ||
rarea y0lb y0ub x0, fcolor(dkgreen%20) lwidth(none) ||
line y1 x1, lcolor(dknavy) ||
line y0 x0, lcolor(dkgreen) lpattern(dash) ||
scatter mY1 mZ, msymbol(Oh) mcolor(dknavy) ||
scatter mY0 mZ, msymbol(Dh) mcolor(dkgreen)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin))
ylab(-0.02(0.01)0.02,nogrid) xlab(-0.5(0.25)0.5,nogrid)
xtitle("Officer Stringency")
legend(ring(0) pos(2) order(5 6) cols(1) region(lstyle(border))
lab(5 "Exposure") lab(6 "No Exposure"))
text(-0.013 -0.3 "`b1'" "`b0'" "`diff'", place(s)) ;
#delimit cr
graph export "${out}/apx_deter/learn_exposure.pdf", replace
// --------------------------------------------------------------------------------------------------



// panel b: heterogeneity by reoffending location --------------------------------------------------

* setup data ----------------------------
use "${data}/out/4-main", clear
keep citationid dlnumber offensedate countynum 
ren countynum COUNTYNUM
joinby dlnumber using "${data}/out/1-offending"

drop if crash == 1
gen cite  = 1
gen speed = (viol==575)
gen nextyear = (date - offensedate>1) & (date - offensedate<=365)
gen pastyear = (offensedate - date>1) & (offensedate - date<=365)
gen Yin  = (`Y'==1) & (nextyear==1) & (countynum==COUNTYNUM)
gen Yout = (`Y'==1) & (nextyear==1) & (countynum!=COUNTYNUM)
collapse (max) Yin Yout, by(citationid) fast
tempfile temp
save    `temp'

use "${data}/out/4-main", clear
gen D = harsh
gen Y = `Y'_ny1
keep citationid D Z totfe officerid
merge 1:1 citationid using `temp', keep(1 3) nogen
replace Yin  = 0 if mi(Yin)
replace Yout = 0 if mi(Yout)
tempfile temp
save    `temp'
/// ------------------------------------------

* compute IV estimates -----------------------
use `temp', clear
ivreghdfe Yin (D=Z), absorb(totfe) vce(cluster officerid)
local bin_print = "Same: {it:{&beta}{subscript:IV}} = `:di %4.3f _b[D]' (`:di %4.3f _se[D]')"
ivreghdfe Yout (D=Z), absorb(totfe) vce(cluster officerid)
local bout_print = "Other: {it:{&beta}{subscript:IV}} = `:di %4.3f _b[D]' (`:di %4.3f _se[D]')"
// ------------------------------------------------


* append data for testing difference --------------
use `temp', clear
gen IN = 1
gen Y  = Yin
tempfile part1
save    `part1'
use `temp', clear
gen IN  = 0
gen Y   = Yout
tempfile part2 
save    `part2'
use `part1', clear
qui append using `part2'

egen totfe_in = group(totfe IN)
egen officerid_in = group(officerid IN)

gen Zin = Z*IN 
gen Din = D*IN

ivreghdfe Y (D Din = Z Zin), absorb(totfe_in) vce(cluster officerid_in)
local bdiff = string(_b[Din],"%4.3f")
local sdiff = string(_se[Din],"%4.3f")
local diff = "{&Delta} = `bdiff' (`sdiff')"
// ---------------------------------------------


* setup for plot -------------------------------
use `temp', clear

* residualize -----------------------
reghdfe Z, absorb(totfe) vce(cluster officerid) nocons resid
predict rZ, resid
reghdfe Yin, absorb(totfe) vce(cluster officerid) nocons resid
predict rYin, resid
reghdfe Yout, absorb(totfe) vce(cluster officerid) nocons resid
predict rYout, resid

* manual binscatter ------------------
local nbin = 20
binscatter rYin rZ, nodraw nq(`nbin') gen(BIN)
gen mZ    = . 
gen mYin  = .
gen mYout = .
forval i = 1/`nbin' {
	qui summ rZ if BIN == `i'
	replace mZ = r(mean) if _n == `i'
	qui summ rYin if BIN == `i'
	replace mYin = r(mean) if _n == `i'
	qui summ rYout if BIN == `i'
	replace mYout = r(mean) if _n == `i'
}

* nonparametric fits --------------
_pctile rZ, n(100)
scalar loZ = r(r2)
scalar hiZ = r(r98)

// fit for in ----
lpoly rYin rZ if rZ>loZ & rZ<hiZ,  nogr ci gen(x1 y1) se(y1se) degree(0) bw(0.2) kernel(tri)
cap g y1lb = y1 - 1.96*y1se
cap g y1ub = y1 + 1.96*y1se

// fit for out ----
lpoly rYout rZ if rZ>loZ & rZ<hiZ, nogr ci gen(x0 y0) se(y0se) degree(0) bw(0.2) kernel(tri)
cap g y0lb = y0 - 1.96*y0se
cap g y0ub = y0 + 1.96*y0se


/// Build plot --------------------
#delimit ;
twoway 
rarea y1lb y1ub x0, fcolor(dknavy%20) lwidth(none)  ||
rarea y0lb y0ub x0, fcolor(dkgreen%20) lwidth(none) ||
line y1 x1, lcolor(dknavy) ||
line y0 x0, lcolor(dkgreen) lpattern(dash) ||
scatter mYin mZ, msymbol(Oh) mcolor(dknavy) ||
scatter mYout mZ, msymbol(Dh) mcolor(dkgreen)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin))
ylab(-0.015(0.0075)0.015,nogrid) xlab(-0.5(0.25)0.5,nogrid)
xtitle("Officer Stringency")
legend(ring(0) pos(2) order(5 6) cols(1) region(lstyle(border))
lab(5 "Same County") lab(6 "Other County"))
text(-0.0075 -0.3 "`bin_print'" "`bout_print'" "`diff'", place(s)) ;
#delimit cr
graph export "${out}/apx_deter/learn_location.pdf", replace
// ------------------------------------------------------------------------------------------------------






